package com.boc.langxiang.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.boc.langxiang.R;
import com.boc.langxiang.entity.bean.ShopBannerInfo;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.boc.langxiang.ui.activity.shop.ProductDetail2Activity;
import com.boc.langxiang.ui.activity.shop.ProductDetailActivity;

import java.util.List;

public class ShopBannerPageAdapter extends PagerAdapter {
    private List<ShopBannerInfo> comm_data_ls;

    public List<ShopBannerInfo> getList() {
        return comm_data_ls;
    }

    public void setList(List<ShopBannerInfo> comm_data_ls) {
        this.comm_data_ls = comm_data_ls;
    }

    private Context context;
    private View itemView;

    public ShopBannerPageAdapter(Context context, List<ShopBannerInfo> comm_data_ls) {
        this.context = context;
        this.comm_data_ls = comm_data_ls;
    }

    @Override
    public int getCount() {
        return comm_data_ls.size() > 0 ? comm_data_ls.size() == 1 ? 1 : Integer.MAX_VALUE : 0;
    }

    public Object instantiateItem(ViewGroup container, int position) {
        itemView = View.inflate(context, R.layout.item_viewpage_content, null);
        ImageView imageView = ((ImageView) itemView.findViewById(R.id.image));
        final int realPosition = position % comm_data_ls.size();
        ShopBannerInfo shopBannerInfo = comm_data_ls.get(realPosition);
        imageView.setTag(shopBannerInfo);
        if (shopBannerInfo != null) {
            String photo = shopBannerInfo.getPhoto();
            PicassoImageLoader.getInstance().displayUrlImage(context, photo, imageView);
        } else {
            PicassoImageLoader.getInstance().displayUrlImage(context, null, imageView);
        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShopBannerInfo shopBannerInfo1 = (ShopBannerInfo) v.getTag();
                String designer_status = shopBannerInfo1.getDesigner_status();
                switch (designer_status) {
                    case "2":
                        Intent intent = new Intent(context, ProductDetailActivity.class);
                        intent.putExtra("id", shopBannerInfo1.getId());
                        context.startActivity(intent);
                        break;
                    case "3":
                        intent = new Intent(context, ProductDetail2Activity.class);
                        intent.putExtra("id", shopBannerInfo1.getId());
                        context.startActivity(intent);
                        break;
                }
            }
        });

        container.addView(itemView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object obj) {
//        container.removeView(itemView);
        container.removeView((View) obj);
    }

    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == (arg1);
    }

}
